package com.freemyleft.left.zapp.app;

/**
 * Created by FreeMyleft on 2017/11/29.
 */

import android.app.Activity;
import android.os.Handler;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * 进行一些配置文件的存储及获取
 */
public class Configurator {
    //创建存储信息的数据结构  weakHashmap 在不使用它的时候自己会进行回收  java代码的编写规范 static final的修饰类他的名字要大写
    private static final HashMap<Object, Object> LEFT_CONFIGS = new HashMap<>();
    private static final Handler HANDLER = new Handler();

    private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();

    private static final ArrayList<Interceptor>INTERCEPTORS=new ArrayList<>();//拦截器


    private Configurator() {
        //枚举类的点name方法就是把这个枚举的值通过字符串输出
        //判断是否配置成功
        LEFT_CONFIGS.put(ConfigKeys.CONFIG_READY, false);

            LEFT_CONFIGS.put(ConfigKeys.HANDLER, HANDLER);

    }

    public static Configurator getInstace() {
        return Hodel.INSTACE;
    }

    final HashMap<Object, Object> getLeftConfigs() {
        return LEFT_CONFIGS;
    }

    /**
     * 静态内部类单例模式的初始化
     */
    private static class Hodel {
        //创建单例
        private static final Configurator INSTACE = new Configurator();

    }

    public final void configure() {
        initIcons();
        LEFT_CONFIGS.put(ConfigKeys.CONFIG_READY.name(), true);
    }

    public final Configurator withApiHost(String host) {

        LEFT_CONFIGS.put(ConfigKeys.API_HOST, host);
        return this;
    }
    public final Configurator withLoaderDelayed(long delayed) {
            LEFT_CONFIGS.put(ConfigKeys.LOADER_DELAYED, delayed);
        return this;
    }

    public final Configurator withIcon(IconFontDescriptor descriptor) {
        ICONS.add(descriptor);
        return this;
    }
    public final Configurator withInterceptor(Interceptor interceptor){
        INTERCEPTORS.add(interceptor);
        LEFT_CONFIGS.put(ConfigKeys.INTERCEPTOR,INTERCEPTORS);
        return this;
    }
    public final Configurator withInterceptor(ArrayList<Interceptor> interceptors){
        INTERCEPTORS.addAll(interceptors);
        LEFT_CONFIGS.put(ConfigKeys.INTERCEPTOR,INTERCEPTORS);
        return this;
    }
    public final Configurator withweChatAppId(String appId){
        LEFT_CONFIGS.put(ConfigKeys.WE_CHAT_APP_ID,appId);
        return this;
    }
    public final Configurator withweChatSecret(String appSecret){
        LEFT_CONFIGS.put(ConfigKeys.WE_CHAT_APP_SECRET,appSecret);
        return this;
    }
    public final Configurator withActivity(Activity activity){
        LEFT_CONFIGS.put(ConfigKeys.ACTIVITY,activity);
        return this;
    }

    //初始化字体图标文件
    private void initIcons() {
        if (ICONS.size() > 0) {//如果集合中有字体了
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for (int i = 1; i < ICONS.size(); i++) {
                initializer.with(ICONS.get(i));

            }

        }
    }

    /**
     * 此处检查配置项是否完成
     */

    private void chekConfiguration() {
        final boolean isReady = (boolean) LEFT_CONFIGS.get(ConfigKeys.CONFIG_READY.name());
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configura 配置未完成");
        }
    }

    @SuppressWarnings("unchecked")
    final <T> T getConfiguration(Object key) {
        chekConfiguration();
        return (T) LEFT_CONFIGS.get(key);

    }
}


